package com.ziyueyuwen.xiniao.util;

import com.aliyun.oss.OSSClient;
import com.aliyun.oss.common.auth.DefaultCredentialProvider;
import com.suyeer.basic.util.DateUtil;
import com.suyeer.basic.util.LogUtil;
import com.ziyueyuwen.xiniao.enums.OssPathEnum;
import org.apache.commons.lang3.RandomUtils;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import java.util.Date;

/**
 * @author jun 2018/12/19.
 */
public class OssUtil {
    private static final String OSS_ACCESS_KEY_ID = "LTAIWMyv39wRmHED";
    private static final String OSS_ACCESS_KEY_SECRET = "Eyjtr2dkuettxPRAzsBDzNnRTz1jNK";
    private static final String OSS_ENDPOINT = "oss-cn-beijing.aliyuncs.com";
    private static final String OSS_INTERNAL_ENDPOINT = "oss-cn-beijing-internal.aliyuncs.com";

    public static String putObjectToOss(File file, OssPathEnum ossPath) {
        return putObjectToOss(file, ossPath, false);
    }

    public static String putObjectToOss(File file, OssPathEnum ossPath, boolean useUniqueFileName) {
        try {
            return putObjectToOss(new FileInputStream(file), file.getName(), ossPath, useUniqueFileName);
        } catch (Exception e) {
            LogUtil.error(e);
        }
        return null;
    }

    public static String putObjectToOss(MultipartFile file, OssPathEnum ossPath) {
        return putObjectToOss(file, ossPath, false);
    }

    public static String putObjectToOss(MultipartFile file, OssPathEnum ossPath, boolean useUniqueFileName) {
        try {
            return putObjectToOss(file.getInputStream(), file.getOriginalFilename(), ossPath, useUniqueFileName);
        } catch (Exception e) {
            LogUtil.error(e);
        }
        return null;
    }

    public static String putObjectToOss(InputStream inputStream, String fileName, OssPathEnum ossPath) {
        return putObjectToOss(inputStream, fileName, ossPath, false);
    }

    public static String putObjectToOss(InputStream inputStream, String fileName, OssPathEnum ossPath, boolean useUniqueFileName) {
        try {
            if (useUniqueFileName) {
                fileName = getUniqueFileName(fileName.toLowerCase());
            }
            String bucketKey = String.format("%s/%s", ossPath.getFolder(), fileName);
            DefaultCredentialProvider credentialProvider = new DefaultCredentialProvider(OSS_ACCESS_KEY_ID, OSS_ACCESS_KEY_SECRET);
            OSSClient ossClient = new OSSClient(OSS_ENDPOINT, credentialProvider, null);
            checkBucketExistInOss(ossClient, ossPath.getBucketName());
            ossClient.putObject(ossPath.getBucketName(), bucketKey, inputStream);
            return getOssFileUrl(fileName, ossPath);
        } catch (Exception e) {
            LogUtil.error(e);
        }
        return null;
    }

    public static String getOssFileUrl(String fileName, OssPathEnum ossPath) {
        return String.format("https://%s.oss-cn-beijing.aliyuncs.com/%s/%s", ossPath.getBucketName(), ossPath.getFolder(), fileName);
    }

    private static void checkBucketExistInOss(OSSClient ossClient, String bucketName) throws Exception {
        if (!ossClient.doesBucketExist(bucketName)) {
            throw new Exception("bucketName不存在: " + bucketName);
        }
    }

    public static String getUniqueFileName(String fileName) {
        return String.format("%s%03d%s",
                DateUtil.getShortTimeFormStr(new Date()),
                RandomUtils.nextInt(0, 1000),
                fileName.substring(fileName.lastIndexOf("."), fileName.length()));
    }

}
